package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.google.common.collect.Lists;

import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.vo.category.CategoryVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;


    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;


    /**
     * 优化
     *
     * @return
     */
    @Override
    public List<CategoryVo> getBaseCategoryList() {
        return baseCategoryViewMapper.getBaseCategoryList();
    }

    @Override
    public List<BaseAttribute> findAttributeAndValue(Long c1Id) {

        return  baseAttributeMapper.findAttributeAndValue(c1Id);
    }


    /**
     * 传统方案
     *
     * @return
     */
//    @Override
//    public List<CategoryVo> getBaseCategoryList() {
//        List<CategoryVo> categoryVoArrayList = new ArrayList<CategoryVo>();
//
//        // 1.查询所有分类数据
//        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
//
//        // 2.封装数据（自己完成）
//        // key:所有的一级分类id  value是这个一级分类下的所有孩子对象集合
//        Map<Long, List<BaseCategoryView>> category1IdAndChildrenMap = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
//        for (Map.Entry<Long, List<BaseCategoryView>> category1IdAndChildrenEntry : category1IdAndChildrenMap.entrySet()) {
//
//            CategoryVo category1Vo = new CategoryVo();
//            category1Vo.setCategoryId(category1IdAndChildrenEntry.getKey()); // 一级分类id
//            List<BaseCategoryView> category1ChildrenList = category1IdAndChildrenEntry.getValue();
//            category1Vo.setCategoryName(category1ChildrenList.get(0).getCategory1Name()); // 一级分类名字
//            // key:某一个一级分类下的所有二级分类id  value是这个二级分类下的所有孩子对象集合
//            Map<Long, List<BaseCategoryView>> category2IdAndChildrenMap = category1ChildrenList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
//            ArrayList<CategoryVo> category2Vos = new ArrayList<>();
//            for (Map.Entry<Long, List<BaseCategoryView>> category2IdAndChildrenEntry : category2IdAndChildrenMap.entrySet()) {
//                CategoryVo category2Vo = new CategoryVo();
//                category2Vo.setCategoryId(category2IdAndChildrenEntry.getKey()); // 二级分类的id
//                List<BaseCategoryView> category2ChildrenList = category2IdAndChildrenEntry.getValue();
//                category2Vo.setCategoryName(category2ChildrenList.get(0).getCategory2Name()); // 二级分类的名字
//                ArrayList<CategoryVo> category3Vos = new ArrayList<>();
//                for (BaseCategoryView baseCategoryView : category2ChildrenList) {
//                    CategoryVo category3Vo = new CategoryVo();
//                    category3Vo.setCategoryId(baseCategoryView.getCategory3Id());
//                    category3Vo.setCategoryName(baseCategoryView.getCategory3Name());
//                    category3Vo.setCategoryChild(Lists.newArrayList());
//                    category3Vos.add(category3Vo);
//                }
//                category2Vo.setCategoryChild(category3Vos); // 二级分类的孩子
//                category2Vos.add(category2Vo);
//            }
//            category1Vo.setCategoryChild(category2Vos); // 一级分类孩子
//            categoryVoArrayList.add(category1Vo);
//
//        }
//
//        return categoryVoArrayList;
//    }
}
